using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using System.Collections.Generic;
using Microsoft.SharePoint.WebPartPages;
using System.Runtime.InteropServices;

namespace PublishingImageWebPart
{
    [Guid("ae02a878-4cc5-4c77-a565-b14ab9ebb1be")]
    public class PublishingImageWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private HtmlImage image = null;
        private HtmlAnchor anchor = null;

        #region ImageUrl
        private string _imageurl = null;

        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        public string ImageUrl
        {
            get { return _imageurl; }
            set { _imageurl = value; }
        }
        #endregion

        #region ImageAlt
        private string _imagealt = null;

        [WebBrowsable(true)]
        [System.ComponentModel.Category("PublishingImage")]
        [Personalizable(PersonalizationScope.Shared)]
        public string ImageAlt
        {
            get { return _imagealt; }
            set { _imagealt = value; }
        }
        #endregion

        #region ImageWidth
        private string _imagewidth = null;

        [WebBrowsable(true)]
        [System.ComponentModel.Category("PublishingImage")]
        [Personalizable(PersonalizationScope.Shared)]
        public string ImageWidth
        {
            get { return _imagewidth; }
            set { _imagewidth = value; }
        }
        #endregion

        #region ImageHeight
        private string _imageheight = null;

        [WebBrowsable(true)]
        [System.ComponentModel.Category("PublishingImage")]
        [Personalizable(PersonalizationScope.Shared)]
        public string ImageHeight
        {
            get { return _imageheight; }
            set { _imageheight = value; }
        }
        #endregion

        #region NavigateUrl
        private string _navigateurl = null;

        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        public string NavigateUrl
        {
            get { return _navigateurl; }
            set { _navigateurl = value; }
        }
        #endregion

        #region NavigateTarget
        private string _navigatetarget = null;

        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        public string NavigateTarget
        {
            get { return _navigatetarget; }
            set { _navigatetarget = value; }
        }
        #endregion

        #region AnchorCssClass
        private string _anchorcssclass = null;

        [WebBrowsable(true)]
        [System.ComponentModel.Category("PublishingImage")]
        [Personalizable(PersonalizationScope.Shared)]
        public string AnchorCssClass
        {
            get { return _anchorcssclass; }
            set { _anchorcssclass = value; }
        }
        #endregion

        public PublishingImageWebPart()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {            
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
        
        protected override void CreateChildControls()
        {
            if (!string.IsNullOrEmpty(NavigateUrl))
            {
                anchor = new HtmlAnchor();
                Controls.Add(anchor);

                image = new HtmlImage();
                anchor.Controls.Add(image);
            }
            else
            {
                image = new HtmlImage();
                Controls.Add(image);
            }

            image.Border = 0;

            base.CreateChildControls();
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (anchor != null)
            {
                anchor.Target = NavigateTarget;
                anchor.HRef = NavigateUrl;
                anchor.Attributes["class"] = AnchorCssClass;
            }

            if (image != null)
            {
                image.Src = ImageUrl;
                image.Alt = ImageAlt;

                int value ;
                if ((!string.IsNullOrEmpty(ImageWidth) && int.TryParse(ImageWidth , out value))) image.Width = value;
                if ((!string.IsNullOrEmpty(ImageHeight) && int.TryParse(ImageHeight, out value))) image.Height = value;
            }
            base.OnPreRender(e);
        }

        public override EditorPartCollection CreateEditorParts()
        {
            AssetUrlSelectorEditorPart lfwbptp = new AssetUrlSelectorEditorPart("ImageUrl", "Select image");
            lfwbptp.AssetPickerButtonVisible = true;
            lfwbptp.UseImageAssetPicker = true;
            lfwbptp.ID = typeof(PublishingImageWebPart).Name + "_ImageUrl";
            List<EditorPart> parts = new List<EditorPart>();
            parts.Add(lfwbptp);

            lfwbptp = new AssetUrlSelectorEditorPart("NavigateUrl", "Select hyperlink destination");
            lfwbptp.AssetPickerButtonVisible = true;
            lfwbptp.ID = typeof(PublishingImageWebPart).Name + "_NavigateUrl";
            parts.Add(lfwbptp);

            AnchorTargetEditorPart atep = new AnchorTargetEditorPart("NavigateTarget", "Select target");
            atep.ID = typeof(PublishingImageWebPart).Name + "_NavigateTarget";
            parts.Add(atep);

            return new EditorPartCollection(base.CreateEditorParts(), parts);
        }
    }
}
